<template>
  <div id="UserAdd">
    <el-card style="width: 98%; height: 40vh" shadow="always">
      <div
        class="category-top"
        style="
          display: flex;
          justify-content: space-between;
          align-items: center;
          line-height: 50px;
          border-bottom: 2px solid rgb(244, 245, 250);
        "
      >
        <h3>修改密码</h3>
      </div>

      <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
      >
        <el-form-item label="原密码" prop="account">
          <el-input v-model="ruleForm.account" style="background-color: #fff" />
        </el-form-item>
        <el-form-item label="新密码" prop="password">
          <el-input
            v-model="ruleForm.password"
            type="password"
            autocomplete="off"
            style="background-color: #fff"
          />
        </el-form-item>
        <el-form-item label="确认新密码" prop="confirmPassword">
          <el-input
            v-model="ruleForm.confirmPassword"
            type="password"
            autocomplete="off"
            style="background-color: #fff"
          />
        </el-form-item>
        <el-form-item>
          <el-button :plain="true" type="primary" @click="submitForm(ruleFormRef)">确定</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { fetchUserCheckOldPwd , fetchUserEditPwd} from '../api/users/index'

const ruleFormRef = ref<FormInstance>()

const ruleForm = reactive({
  account: '',
  password: '',
  confirmPassword: '',
})

const rules = reactive<FormRules<typeof ruleForm>>({
  account: [
    {
      validator: async (rule: any, value: string, callback: any) => {
        const data = {
          oldPwd: value,
        }
        const res = await fetchUserCheckOldPwd(data)
        if (res.code === '11') {
          callback(new Error('原密码错误'))
        } else if (value === '') {
          callback(new Error('原密码不能为空'))
        } else {
          callback()
        }
      },
      trigger: 'blur',
    },
  ],
  password: [
    {
      validator: (rule: any, value: any, callback: any) => {
        if (value === '') {
          callback(new Error('请输入新密码'))
        } else {
          callback()
        }
      },
      trigger: 'blur',
    },
  ],
  confirmPassword: [
    {
      validator: (rule: any, value: any, callback: any) => {
        if (value === '') {
          callback(new Error('请确认新密码'))
        } else if (value !== ruleForm.password) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur',
    },
  ],
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(async (valid) => {
    if (valid) {
      console.log(ruleForm)
      const data = {
        newPwd:ruleForm.password
      }
      //进行修改密码验证
      const res = await fetchUserEditPwd(data)
      console.log("🚀 ~ formEl.validate ~ res:", res)
    }
  })
}
</script>

<style scoped>
.el-input__inner {
  background-color: #fff;
}
/* 取消input死样式 */
:deep(.el-input__inner) {
  -webkit-text-fill-color: #857f7f;
  caret-color: #eee;
  box-shadow: inset 0 0 0 1100px #fff !important;
}
:deep(input::-webkit-input-placeholder) {
  -webkit-text-fill-color: #5f757f !important;
}
</style>
